<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['pcd']=$_SESSION['pcd']?:array();
$_SESSION['area_code']=$_RQ['area_code']?:$_SESSION['area_code'];
$return['area_code']=$_SESSION['area_code'];
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['indexpai']=array();	
	$return['shopset'] = plugin('jy_weishop')->plugin_setting('shopset');
	$return['indexpai'][]=array('name'=>'coupon');
	$return['indexpai'][]=array('name'=>'goods');
	$return['setting']['pluginnav']=$return['pluginset']['bankuai'];
	$return['setting']['hdps']=$return['pluginset']['hdps'];
	$p_group=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1','isshow'=>'1','parentid'=>'0'),'','paixu asc');
	//子分类
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$params['saletype likes']=array('1');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','image','price','total','active_plugin','active_id');
	if($p_group){
		$i=0;
		foreach($p_group as &$item){
			$item['c_group']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','isrecommand'=>'1','parentid'=>$item['id']),'','paixu asc');
			$item['goodslist']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('1'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
			$item['goodsnew']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('2'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
			$item['style']=style($item['id']);
			$i=$i+1;
			$item['i']=$i;
			if($item['c_group']){
				foreach($item['c_group'] as &$row){
					$params['ccate']=$row['id'];
					$row['goods']=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc');
				}
			}
		}
	}
	$parameter=array(
		//'table'=>'jy_weishop_goods',
		'fields'=>array('id','title','image','marketprice','price','total','salenum'),
		'orderby'=>'paixu desc',
		'limit'=>'5',
	);
	//
	$parameter['params']=array('pluginname'=>'jy_pddke','groupnum >'=>0,'groupprice >'=>0,'saletype likes'=>array('1'),'uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$recommand_list=model('jy_weishop/goods')->goods_list($parameter);
	//热卖
	$parameter['params']=array('pluginname'=>'jy_pddke','groupnum >'=>0,'groupprice >'=>0,'saletype likes'=>array('3'),'uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$hot_list=model('jy_weishop/goods')->goods_list($parameter);
	
	$time_list=model('jy_weishop/activity/limit')->goods_list(5);
	//公告列表
	$notice_list = pdo_fetchall('SELECT * FROM ' . tablename('jy_weishop_notice') . ' WHERE uniacid=:uniacid AND status=1  ORDER BY id DESC limit 4', array(':uniacid' => $_SESSION['uniacid']));
	if($notice_list){
		foreach($notice_list as &$notice){
			$notice['createtime']=date('Y-m-d h:i',$notice['createtime']);
		}
	}
	$pindex = max(1, intval($post['page']));
	$psize = 10;
	$total='';
	$return['orderlist']=pdo_getslice('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),array($pindex,$psize),$total,'','','createtime desc');
	foreach($return['orderlist'] as &$ov){
		$ordergoods=pdo_get('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$ordergoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
		$ov['goods']=array_merge($ordergoods,$goodsinfo);
		$ov['createtime']=date('Y/m/d H:i:s',$ov['createtime']);
		$ov['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),'total');
	}
	$return['allprice']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),'price');
	//获取用户信息
	$return['indexrecommands']=$indexrecommands;
	$return['extradata']=array();
	$return['user']=model('core/member')->basic($_SESSION['uid']);
	$return['login']=$_SESSION['uid']?1:2;
	$return['recommand_list']=$recommand_list;
	$return['hot_list']=$hot_list;
	$return['notice_list']=$notice_list;
	$return['p_group']=$p_group;
	$return['params']['pid']=0;
	//刷新当前用户地址
	if(($_SESSION['pcd'] && $_SESSION['pcd']['city']) || $_SESSION['area_code']){
		if(!$_SESSION['area_code']){
			//$return['store']=$this->model()->get_area_code($_SESSION['pcd']);
			$return['area_code']=$_SESSION['area_code']=$return['store']['area_code'];
			$return['store']['members']=pdo_count('jy_weishop_member',array('area_code'=>$return['store']['area_code']),'id');
			$return['store']['salenum']=pdo_getsum('jy_weishop_order',array('area_code'=>$return['store']['area_code']),'ispay');
		}else{
			$return['store']['members']=pdo_count('jy_weishop_member',array('area_code'=>$_SESSION['area_code']),'id');
			$return['store']['salenum']=pdo_getsum('jy_weishop_order',array('area_code'=>$_SESSION['area_code']),'ispay');
		}
		$return['store']['saletime']=date('Y-m-d',time());
		$gettime=$return['pluginset']['gettime']*24*36000+time();
		$return['store']['gettime']=date('Y-m-d',$gettime);
		$return['sharedata']=array(
				'title'=>$return['store']['title'].$return['pluginset']['title'],
				'desc'=>$return['pluginset']['desc'],
				'link'=>custom_url('jy_pddke/index//area_code='.$_SESSION['area_code'].'&fromuser='.$_SESSION['uid'],true),
				'imgUrl'=>ATTACHMENT_URL.$return['pluginset']['storelogo'],
		);
	}
	set_html('title',$return['pluginset']['title']?'首页-'.$return['pluginset']['title']:'首页-拼单商城');
	$return['sharedata']=array(
		'title'=>$return['pluginset']['share_title']?$return['pluginset']['share_title']:'首页-拼单商城',
		'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'拼单商城单商户版，主流经典拼单商城，拼着买，更便宜！',
		'link'=>custom_url('jy_pddke/index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>$return['pluginset']['share_image']?tomedia($return['pluginset']['share_image']):tomedia($return['pluginset']['icon'])
	);
	if(empty($return['orderlist'])){
		$return['coupon_wait']=model('jy_weishop/task')->pop_coupon_wait();
		if(!$return['coupon_wait']){
			$return['coupon_hd']=model('jy_weishop/task')->pop_coupon_hd();
			if(!$return['coupon_hd']){
				$return['game_wait']=model('jy_weishop/task')->pop_game();
			}
		}
	}	
	$return['shopset']=plugin('jy_weishop')->plugin_setting('shopset');
	$return['setting']['nokefu']=$return['shopset']['nokefu'];
	exi($return);
}
if($_RQ['op']=='getarea'){
	//获取当前区域经销商
	$postdata=get_postdata();
	$pcd = $postdata['pcd'];
	if(!$pcd['province'] || !$pcd['city'] || !$pcd['district']){
		return false;
	}
	$_SESSION['pcd']=$pcd;	
	$return['store']=$this->model()->get_area_code($pcd);
	$return['store']['saletime']=date('Y-m-d',time());
	$gettime=$return['pluginset']['gettime']*24*3600+time();
	$return['store']['gettime']=date('Y-m-d',$gettime);
	$return['store']['members']=pdo_count('jy_weishop_member',array('area_code'=>$return['store']['area_code']),'id');
	$return['store']['salenum']=pdo_getsum('jy_weishop_order',array('area_code'=>$return['store']['area_code']),'ispay');
	$return['sharedata']=array(
		'title'=>$return['store']['title'].$return['pluginset']['title'],
		'desc'=>$return['pluginset']['desc'],
		'link'=>custom_url('jy_pddke/index//area_code='.$return['store']['area_code'].'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['pluginset']['storelogo'],
	);
	exi($return);
}

function style($id)
{
	switch ($id%7) {	
		case 0: return "style-brown";
		case 1: return 'style-red';
		case 2: return "style-pink";
		case 3: return "style-orange";
		case 4: return "style-green";
		case 5: return "style-blue";
		case 6: return "style-purple";
		case 7: return "style-brown";
		default: }
}
	
?>